Spring Security | Note-18

Spring Security Note-18


总结

1.引入依赖(pom.xml)

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-browser</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-authorize</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

2.配置系统(参见 application-example.properties)

1
application.properties

3.增加UserDetailsService接口实现

1
2
3
4
5
6
7
8
9
10
@Component
public class TestUserDetailsService implements UserDetailsService{
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return new User(username,passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
}
}

4.如果需要记住我功能,需要创建数据库表(参见 db.sql)

1
2
3
4
5
6
7
-- 记住我功能用的表
create table persistent_logins (
username varchar(64) not null,
series varchar(64) primary key,
token varchar(64) not null,
last_used timestamp not null
);

5.如果需要社交登录功能,需要以下额外的步骤
1).配置appId和appSecret(qq & weixin)

1
2
3
4
# 微信登录配置,参见WeixinProperties
imooc.security.social.weixin.app-id = wxd99431bbff8305a0
imooc.security.social.weixin.app-secret = 60f78681d063590a469f1b297feff3c4
#imooc.security.social.weixin.providerId = weixin

2).创建并配置用户注册页面,并实现注册服务(需要配置访问权限),注意在服务中要调用ProviderSignInUtils的doPostSignUp方法

1
2
3
4
5
6
7
8
@Component
public class TestAuthorizeConfigProvider implements AuthorizeConfigProvider {
@Override
public boolean config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
config.antMatchers(HttpMethod.POST,"/user/regist").permitAll();
return false;
}
}

3).添加SocialUserDetailsService接口实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
public class TestUserDetailsService implements UserDetailsService,SocialUserDetailsService{
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return new User(username,passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
}

@Override
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
return new SocialUser(userId,passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
}
}

4).创建社交登录用的表 (参见 db.sql)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- 社交登录用的表
create table imooc_UserConnection (
userId varchar(255) not null,
providerId varchar(255) not null,
providerUserId varchar(255),
rank int not null,
displayName varchar(255),
profileUrl varchar(512),
imageUrl varchar(512),
accessToken varchar(512) not null,
secret varchar(512),
refreshToken varchar(512),
expireTime bigint,
primary key (userId, providerId, providerUserId));
create unique index UserConnectionRank on imooc_UserConnection(userId, providerId, rank
);